package cn.tede.file;

import java.io.*;

public class TestIn {

    public static void main(String[] args) {
        //  method();
        method2();
    }

    private static void method2() {
        InputStream inputStream = null;
      /*  try {
            inputStream=new BufferedInputStream(new FileInputStream("G:\\Project01\\试题\\1.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
        try {
            inputStream = new BufferedInputStream(new FileInputStream(new File("G:\\Project01\\试题\\1.txt")));
            int b=0;
            while ((b=inputStream.read())!=-1){
                System.out.println((char) b);
            }

        } catch (Exception e) {
            System.out.println("文件丢失");
        }finally {
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static void method() {
        //局部变量需要初始化
        InputStream inputStream = null;
        try {
            /*使用第一种构造器*/
            //  InputStream = new FileInputStream(new File("G:\\Project01\\试题\\1.txt")); //
            /*使用第二种构造器*/
            inputStream = new FileInputStream("G:\\Project01\\试题\\1.txt");
/*            System.out.println( inputStream.read());
            System.out.println( inputStream.read());
            System.out.println( inputStream.read());
            System.out.println( inputStream.read());*/
            //循环读取文件中的所有内容
            /*每次调用read方法都会读取一次*/
            int b = 0;
            while ((b = inputStream.read()) != -1) {
                System.out.println(b);
            }
        } catch (IOException e) {
            System.out.println("文件丢失");
            /*无论是否捕获到异常都会执行*/
        } finally {
            if (inputStream != null) {

                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
